package com.zackeus.common.utils;

/**
*
* @ClassName: FormatUtils
* @Description: 格式工具类
* @Author: zz
* @Date: 2020/12/29 16:01
*/
public class FormatUtils {

    public static final char FORMAT_CHAR_HOLDER_PLACE = '#';    /** 格式占位符 */
    public static final char FORMAT_CHAR_SEP = '-';             /** 格式分隔符 */
    public static final char DEFAULT_CHAR_SEP = ' ';            /** 默认分隔符 */

    public static final String FORMAT_CLEAR_PHONE = "###-####-####";
    public static final String FORMAT_CLEAR_BANK_CARD = "####-####-####-####-####-####";

    private FormatUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static String getPhone(final String content) {
        return getPhone(content, FORMAT_CLEAR_PHONE, DEFAULT_CHAR_SEP);
    }

    public static String getPhone(final String content, final String phoneFormat) {
        return getPhone(content, phoneFormat, DEFAULT_CHAR_SEP);
    }

    /**
     * 获取清晰格式的手机号码
     * @param content
     * @param phoneFormat  自定义输入格式
     * @param sepChar  自定义分隔符
     * @return
     */
    public static String getPhone(final String content, final String phoneFormat, final char sepChar) {
        return getFormat(content, phoneFormat, sepChar);
    }

    public static String getBankCard(final String content) {
        return getBankCard(content, FORMAT_CLEAR_BANK_CARD, DEFAULT_CHAR_SEP);
    }

    public static String getBankCard(final String content, final String bankCardFormat) {
        return getBankCard(content, bankCardFormat, DEFAULT_CHAR_SEP);
    }

    /**
     * 获取清晰格式的银行卡号
     * @param content
     * @param bankCardFormat  自定义输入格式
     * @param spliterChar  自定义分隔符
     * @return
     */
    public static String getBankCard(final String content, final String bankCardFormat, final char spliterChar) {
        return getFormat(content, bankCardFormat, spliterChar);
    }

    public static String getFormat(final String content, final String format, final char spliterChar) {
        if (StringUtils.isBlank(content)) {
            return content;
        }
        validateFormat(format);

        int formatLen = format.length();
        StringBuilder ret = new StringBuilder(content);

        for (int i = 0; i < ret.length(); i++) {
            if (i <= formatLen - 1) {
                char formatCh = format.charAt(i);
                if (formatCh == FORMAT_CHAR_SEP) {
                    ret.insert(i, spliterChar);
                }
            }
        }
        return ret.toString();
    }

    /**
     * 验证格式是否正确
     * @param formatStr
     */
    private static void validateFormat(final String formatStr) {
        if (StringUtils.isBlank(formatStr)) {
            throw new IllegalArgumentException("The formatStr must be not empty!");
        }

        for (int i = 0; i < formatStr.length(); i++) {
            if (formatStr.charAt(i) != FORMAT_CHAR_HOLDER_PLACE && formatStr.charAt(i) != FORMAT_CHAR_SEP) {
                throw new IllegalArgumentException("The string must include only '#' or '-' char!");
            }
        }
    }
}
